<?php

include_once './util/db/Conn.php';
include_once './util/ArrayList.php';
include_once './util/db/model/ProdAttr.php';
include_once './util/db/model/OrderDetail.php';

class ProdAttrDBUtil {
	protected $dbConn;
	private $tabName = "prod_attr";
	function __construct() {
		$this->dbConn = new DBConn ();
		@header ( "Content-Type:text/html; charset=utf-8" );
	}
	
	public function open() {
		$this->dbConn->open ();
	
	}
	
	public function close() {
		$this->dbConn->close ();
	}
	
	public function queryAll() {
		$this->open ();
		$arrayList = new ArrayList ();
		$sql = "select * from `$this->tabName`";
		$result = mysql_query ( $sql );
		while ( $row = @mysql_fetch_array ( $result ) ) {
			$arrayList->add ( $this->fill ( $row ) );
		}
		$this->close ();
		return $arrayList;
	}
	
	/**
	 * Enter description here ...
	 */
	private function fill($row) {
		$prodAttr = new ProdAttr ();
		$prodAttr->setPa_id ( $row ['pa_id'] );
		$prodAttr->setP_id ( $row ['p_id'] );
		$prodAttr->setCount ( $row ['count'] );
		$prodAttr->setAttr ( $row ['attr'] );
		return $prodAttr;
	}
	
	public function queryByPid($pid) {
		$this->open ();
		$arrayList = new ArrayList ();
		$sql = "select * from `$this->tabName` where P_id=$pid ";
//		echo $sql;
		$result = mysql_query ( $sql );
		while ( $row = @mysql_fetch_array ( $result ) ) {
			$arrayList->add ( $this->fill ( $row ) );
		}
		$this->close ();
		return $arrayList;
	}
	
	public function insertNewDate($prodAttr) {
		$this->open ();
		$sql = "insert into `$this->tabName` (p_id,count,attr) values(" . $prodAttr->getP_id () . ", " . $prodAttr->getCount () . ",'" . $prodAttr->getAttr () . "')";
		$result = mysql_query ( $sql );
		$this->close ();
	}
	
	public function updateDate($prodAttr) {
		$this->open ();
		$sql = "update `$this->tabName` set count=" . $prodAttr->getCount () . ",attr='" . $prodAttr->getAttr () . "' where pa_id=" . $prodAttr->getPa_id ();
		mysql_query ( $sql );
		$this->close ();
	}
	
	public function setOrderCheckOut($orderDeatilArrayList) {
		$success = true;
		$this->open ();
		$sql = "START TRANSACTION";
		mysql_query ( $sql );
		for($i = 0; $i < $orderDeatilArrayList->size (); $i ++) {
			$orderDetail = $orderDeatilArrayList->get ( $i );
			$sql = "select * from prod_attr where pa_id=" . $orderDetail->getProdAttr ()->getPa_id ();
			$prodAttr = null;
			$result = mysql_query ( $sql );
			while ( $row = @mysql_fetch_assoc ( $result ) ) {
				$prodAttr = $this->fill ( $row );
			}
			if($prodAttr->getCount()>=$orderDetail->getCount()){
				$sql="update `$this->tabName` set count=" . ($prodAttr->getCount ()-$orderDetail->getCount()) . " where pa_id=" . $prodAttr->getPa_id ();
				mysql_query ( $sql );
			}else{
				$success=false;
				$sql="rollback";
				mysql_query ( $sql );
				break;
			}
			if($i==$orderDeatilArrayList->size ()-1){
				$sql="commit";
				mysql_query ( $sql );
			}
		}
		$this->close ();
		return $success;
	}
}
?>